<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width,
           initial-scale=1.0,
          maximum-scale=1.0,
           minimum-scale=1.0,
           user-scalable=no">
    <meta name="data-spm" content="1" data-spm-protocol="i" />
    <title>{{component}} test runner</title>
    <link rel="stylesheet" href="/kissy/tools/jasmine/jasmine.css">
    <link rel="stylesheet" href="/kissy/build/css/dpl/base.css"/>

    {{#each externalLinks}}
        <link rel="stylesheet" href="{{{.}}}"/>
    {{/each}}

    <style>
        {{{externalStyle}}}
    </style>
    {{#if query.min}}
        <script src="/kissy/build/seed-min.js"></script>
    {{else}}
        <script src="/kissy/build/seed.js"></script>
    {{/if}}
    <script>
        var loc=new KISSY.Uri(location.href);
        var component= '{{{component}}}';
        if(loc.getQuery().has('coverage')){
            KISSY.config('map',[[/kissy\/src\/(.+)\/src\//g,function(m,m1){
                var mm=m1.replace(/sub-modules\//g,'');
                if(mm!=component) return m;
                return m.slice(0,-5)+'/coverage/src/';
            }]]);
        }
    </script>
    <script src="/kissy/src/package.js"></script>
    <script src="/kissy/tools/jasmine/jasmine.js"></script>
    <script src="/kissy/tools/jasmine/jasmine-html.js"></script>
    <script src="/kissy/tools/jasmine/jasmine.console_reporter.js"></script>
    <script src="/kissy/tools/jasmine/event-simulate.js"></script>
    {{#each externalScripts}}
        <script src="{{{.}}}"></script>
    {{/each}}
</head>
<body>
<h1 style="text-align: center">{{component}} test cases</h1>
{{{html}}}
<script>
    var tests=['specs/'], S=KISSY;

    S.config('packages',{
        specs:{
            debug:1,
            base:'../'
        },
        src:{
            debug:1,
            base:'/kissy/'
        }
    });

    {{#if tests}}
        tests.shift();
    {{/if}}

    {{#each tests}}
        tests.push('specs/{{{.}}}');
    {{/each}}

    {{{script}}}

    S.use(tests, function (S) {
        jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
        jasmine.getEnv().addReporter(new jasmine.ConsoleReporter());
        jasmine.getEnv().execute(function () {
            var self=this;
            function createRequest(){
                if (window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
                else {
                    return new XMLHttpRequest();
                }
            }
            if(window.jscoverage_serializeCoverageToJSON && S.UA.phantomjs){
                var json = window.jscoverage_serializeCoverageToJSON();
                var request = createRequest();
                request.open('post', '/save-coverage-report', true);
                request.setRequestHeader('content-type','application/x-www-form-urlencoded; charset=UTF-8');
                request.onreadystatechange = function () {
                    if (request.readyState === 4 && request.status == 200) {
                        jasmine.kissyNext(self.results().failedCount);
                    }
                };
                request.send(S.param({
                    report:json,
                    component:component,
                    path:location.pathname
                }));
            }else {
                jasmine.kissyNext(self.results().failedCount);
            }
        });
    });
</script>
{{{include './footer'}}}
</body>
</html>